package org.uni_hi.lsep;

import java.awt.Image;
import java.io.IOException;
import java.lang.Thread.State;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.uni_hi.lsep.gui.BrowserFunction;
import org.uni_hi.lsep.gui.MainFrame;
import org.uni_hi.lsep.gui.MainFrame.ContentPanelType;
import org.uni_hi.lsep.gui.test.sim.RuleCheckStatusDialog;
import org.uni_hi.lsep.gui.util.Config;
import org.uni_hi.lsep.test.sim.FirewallSimulation;
import org.uni_hi.lsep.util.FileManager;

import com.ctreber.aclib.image.ico.ICOFile;

public class AppManager {

	private static MainFrame _mFrame=null;

	private static FileManager _fileMan=null;

	private static JFileChooser _jfc;

	private static List<Image> _icons;

	private static BrowserFunction _browser;

	private static FirewallSimulation _fwSim=null;

	private static RuleCheckStatusDialog _rcsd = null;

	public AppManager(){
		org.apache.log4j.Logger.getLogger("com.ctreber").setLevel(org.apache.log4j.Level.WARN);
		_mFrame=new MainFrame();
		_fileMan=new FileManager();
		_fwSim=new FirewallSimulation();
		_fwSim.start();
	}

	public void showWindow(){
		synchronized(_mFrame){
			_mFrame.getFrame().setVisible(true);
			_mFrame.getIndexTree().setSelectionRow(0);
		}
	}

	public static MainFrame getMainFrame(){
		return _mFrame;
	}

	public static FileManager getFileManager(){
		return _fileMan;
	}

	public static RuleCheckStatusDialog getRuleCheckStatusDialog(boolean newDialog){
		if(_rcsd==null||newDialog){
			if(newDialog)java.util.logging.Logger.getLogger(AppManager.class.getName()).info("This method is only meant for testing purposes!");
			_rcsd=new RuleCheckStatusDialog(getMainFrame().getFrame());
		}
		return _rcsd;
	}
	
	public static RuleCheckStatusDialog getRuleCheckStatusDialog(){
		return getRuleCheckStatusDialog(false);		
	}

	public static JFileChooser getFileDialog(){
		if(_jfc==null){
			_jfc=new JFileChooser();
			_jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
			_jfc.setFileFilter(new FileNameExtensionFilter("Fw-Ls-Simulationseinstellungsdatei", "fsl"));
		}
		return _jfc;
	}

	public static void saveSimData(){
		if(getFileDialog().showSaveDialog(_mFrame.getContentScrollPane())==JFileChooser.APPROVE_OPTION){
			if(getFileManager().saveSettingsToFile(getFileDialog().getSelectedFile())==1){
				getMainFrame().setStatus("Daten gespeichert");
			}else{
				getMainFrame().setStatus("FEHLER: Daten konnten nicht gespeichert werden!");
			}
		}
	}

	public static void loadSimData(){
		if(getFileDialog().showOpenDialog(_mFrame.getContentScrollPane())==JFileChooser.APPROVE_OPTION){
			getFileManager().loadSettingsFile(getFileDialog().getSelectedFile());
		}
	}

	@SuppressWarnings("unchecked")
	public static List<Image> getImageIcons(){
		if(_icons==null){
			try {
				_icons= new ICOFile(Config.windowIcon).getImages();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return _icons;
	}

	public static ImageIcon getImageIcon(){
		return getImageIcon(0);
	}

	public static ImageIcon getImageIcon(int pos){
		return new ImageIcon(getImageIcons().get(pos));
	}

	public static BrowserFunction getBrowser() {
		if(_browser==null){
			_browser=new BrowserFunction();
		}
		return _browser;
	}

	public static void testRuleset(){
		synchronized (_fwSim) {
			_fwSim.notify();
		}
	}
	
	public static boolean isRulesetTestActive(){
		return _fwSim.getState()!=State.WAITING;
	}

	public static void pauseRulesetTest(){
		_fwSim.waitForIt();
	}

	public static void resumeRulesetTest(){
		_fwSim.goForIt();
	}

	public static void cancelRulesetTest(){
		_fwSim.cancelIt();
	}
	
	public static void setFwSimWaitingTime(int time){
		_fwSim.setWaitingTime(time);
	}

	public static void openFirewallSimulation() {
		getMainFrame().setContentPanelType(ContentPanelType.TEST);
		getMainFrame().getTestPanel().addSimPanel(true);
		getMainFrame().getMainMenuBar().setLoadSaveEnabled(true);
		getMainFrame().getNavigationbar().setTitle("Freie Firewallsimulation");
	}

	public static void clearSimData() {
		getMainFrame().getTestPanel().getAssignmentTabbedPane().removePackets();
		getMainFrame().getTestPanel().getSimsettingsPanel().removeRuleRows();
	}

	public static Long getFwSimWaitingTime() {
		return _fwSim.getWaitingTime();
	}
}
